package com.marlon.auto.controller;

import com.marlon.auto.common.base.entity.QueryEntity;
import com.marlon.auto.common.base.entity.RespMessage;
import com.marlon.auto.entity.mapper.AreaInfoMapper;
import com.marlon.auto.entity.vo.AreaInfoVO;
import com.marlon.auto.service.AreaInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author marlon
 * @since 2022-04-19
 */
@Api(tags = "区域管理")
@Slf4j
@RestController
@RequestMapping("/areaInfo")
public class AreaInfoController {

    AreaInfoMapper mapper = AreaInfoMapper.INSTANCE;
    @Autowired
    public AreaInfoService areaInfoService;

    @ApiOperation(value = "区域管理查询列表", notes = "区域管理查询列表")
    @PostMapping("/queryLists")
    public Page<AreaInfoVO> queryLists(@RequestBody QueryEntity<AreaInfoVO> queryEntity){
        Pageable page = queryEntity.getPage();
        Page<AreaInfoVO> result = areaInfoService.findAllByVOAndKeyword(queryEntity.getQuery(), page);
        //处理返回的状态码值
        result.getContent().stream().forEach(item -> {

        });
        return result;
    }

    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/add")
    public RespMessage add(@RequestBody AreaInfoVO vo) {
        try {
            return RespMessage.ok(mapper.poToVO(areaInfoService.add(mapper.voToPO(vo))));
        } catch (Exception e) {
            log.error("新增区域异常：{}",e);
            return RespMessage.error("新增异常，请稍候重试！");
        }
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public RespMessage update(@RequestBody AreaInfoVO vo) {
        try {
            return RespMessage.ok(mapper.poToVO(areaInfoService.update(mapper.voToPO(vo), vo.getAreaId())));
        } catch (Exception e) {
            log.error("修改区域异常：{}",e);
            return RespMessage.error("修改异常，请稍候重试！");
        }
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping("/batchDelete")
    public RespMessage batchDelete(@RequestParam("ids") List<String> ids) {
        try {
            if (CollectionUtils.isEmpty(ids)){
                return RespMessage.error("请至少选择一行数据");
            }
            areaInfoService.delete(ids);
            return RespMessage.ok();
        } catch (Exception e) {
            log.error("删除区域异常：{}",e);
            return RespMessage.error("删除异常，请稍候重试！");
        }
    }

    @ApiOperation(value = "区域下拉框", notes = "区域下拉框")
    @GetMapping("/select")
    public RespMessage areaSelect() {
        return RespMessage.ok(areaInfoService.findAllAvailable());
    }

}